package com.dinggao.crossgo.module.roadbook.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.dinggao.crossgo.R;
import com.dinggao.crossgo.core.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class RoadBookTrackDetailsActivity extends BaseActivity {

    @BindView(R.id.iv_top_bar_left)
    ImageView ivTopBarLeft;
    @BindView(R.id.tv_top_bar_title)
    TextView tvTopBarTitle;
    @BindView(R.id.iv_top_bar_right)
    ImageView ivTopBarRight;
    @BindView(R.id.track_mapview)
    MapView mMapView;

    private AMap aMap;
    private Polyline polyline;
    private List<LatLng> pathList = new ArrayList<>();
    private ArrayList<MarkerOptions > markerList = new ArrayList<>();


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_road_book_track_details;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        mMapView.onCreate(bundle);
        tvTopBarTitle.setText("轨迹详情");
        ivTopBarRight.setImageDrawable(getResources().getDrawable(R.drawable.road_book_parent_share_icon));

        //初始化地图控制器对象
        if (aMap == null) {
            aMap = mMapView.getMap();
        }

        pathList = ( List<LatLng>)getIntent().getSerializableExtra("pathList");


        markerList.add(new MarkerOptions().position(pathList.get(0)));
        markerList.add(new MarkerOptions().position(pathList.get(pathList.size() - 1)));

        aMap.addMarkers(markerList, true);

        // 初始化轨迹
        PolylineOptions polylineOptions = new PolylineOptions();
        polylineOptions.width(16);
        polylineOptions.color(Color.parseColor("#ffd219"));
        polyline = aMap.addPolyline(polylineOptions);
        polyline.setPoints(pathList);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @OnClick({R.id.iv_top_bar_left, R.id.iv_top_bar_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_top_bar_left:
                finish();
                break;
            case R.id.iv_top_bar_right:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }
}
